<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/26
 * Time: 15:26
 */

namespace Platform\Models\Admin;

use Platform\Models\BaseModel;

class AdminRole extends BaseModel
{
    const TABLE_NAME = 'admin_role';

    public function initialize()
    {
        parent::initialize();
        $this->pk = "id";
        $this->setSource(self::TABLE_NAME);
    }

    private $id;
    private $name;
    private $pid;
    private $status;
    private $remark;
    private $ename;
    private $updateTime;
    private $createTime;

    public function columnMap() {
        //列名 =》 属性名
        return [
            'id' => 'id',
            'name' => 'name',
            'pid' => 'pid',
            'status' => 'status',
            'remark' => 'remark',
            'ename' => 'ename',
            'update_time' => 'updateTime',
            'create_time' => 'createTime',
        ];
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getPid()
    {
        return $this->pid;
    }

    /**
     * @param mixed $pid
     */
    public function setPid($pid)
    {
        $this->pid = $pid;
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    /**
     * @return mixed
     */
    public function getRemark()
    {
        return $this->remark;
    }

    /**
     * @param mixed $remark
     */
    public function setRemark($remark)
    {
        $this->remark = $remark;
    }

    /**
     * @return mixed
     */
    public function getEname()
    {
        return $this->ename;
    }

    /**
     * @param mixed $ename
     */
    public function setEname($ename)
    {
        $this->ename = $ename;
    }

    /**
     * @return mixed
     */
    public function getUpdateTime()
    {
        return $this->updateTime;
    }

    /**
     * @param mixed $updateTime
     */
    public function setUpdateTime($updateTime)
    {
        $this->updateTime = $updateTime;
    }

    /**
     * @return mixed
     */
    public function getCreateTime()
    {
        return $this->createTime;
    }

    /**
     * @param mixed $createTime
     */
    public function setCreateTime($createTime)
    {
        $this->createTime = $createTime;
    }

    /**
     * 查询所有的角色
     * @return mixed
     */
    public function getRoleAll(){
        $adminRole = parent::find();
        return $adminRole->toArray();
    }


    /**
     * 通过名称和角色查询
     * @param $role
     * @return mixed
     */
    public function getRoleByRemarkAndName($name, $role)
    {
        $adminRole = parent::findFirst([
            'conditions' => 'name = :name: and remark = :remark:',
            'bind' => [
                'name' => $name,
                'remark' => $role,
            ]
        ]);
        return $adminRole;
    }

    /**
     * 添加角色
     */
    public function addRole($name, $role)
    {
        $this->name = $name;
        $this->remark = $role;
        $this->updateTime = time();
        $this->createTime = time();

        // 插入
       $this->save();
    }

    /**
     * 通过角色描述查询角色
     * @param $remark
     */
    public function getRoleByRemark($remark){
        $roleId = parent::findFirst(
            [
                'conditions' => 'remark = :remark:',
                'bind' => [
                    'remark' => $remark
                ]
            ]
        );
        return $roleId;
    }



}